﻿Imports System.Text



Namespace Core

    ''' <summary>
    ''' 扩展名管理器
    ''' </summary>
    ''' <remarks>
    ''' 用于向对话框提供显示文件过滤字符串功能
    ''' 以及检查文件是否适合打开
    ''' </remarks>
    Public Module FileExtensionManager

        ''' <summary>
        ''' 本地资源管理器
        ''' </summary>
        ''' <remarks></remarks>
        Private resourceMan As Global.System.Resources.ResourceManager


        Private ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
            Get
                If Object.ReferenceEquals(resourceMan, Nothing) Then
                    Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("iConverter.FileExtensionManagerResource", GetType(FileExtensionManager).Assembly)
                    resourceMan = temp
                End If
                Return resourceMan
            End Get
        End Property




        ''' <summary>
        ''' 文件扩展名过滤字符串
        ''' </summary>
        ''' <remarks></remarks>
        Dim _filterstr As String


        ''' <summary>
        ''' 获取文件扩展名过滤字符串
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property Filter() As String
            Get
                If String.IsNullOrEmpty(_filterstr) Then
                    _filterstr = MakeFilterString()
                End If
                Return _filterstr
            End Get
        End Property

        ''' <summary>
        ''' 获取过滤字符串
        ''' </summary>
        ''' <param name="typename"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function GetFilterString(ByVal TypeName As String) As String
            Return String.Format("{0} (*{1})|*{1}", ResourceManager.GetString(TypeName), TypeName)
        End Function
        ''' <summary>
        ''' 生成文件扩展名过滤字符串
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function MakeFilterString() As String
          
            Dim list = SettingManager.Settings.KnowFileExtensions.Cast(Of String).ToList

            list.Add(".*")

            Dim parts = list.Select(Function(x) GetFilterString(x))

            Dim str = String.Join("|", parts)

            Return str
        End Function


        ''' <summary>
        ''' 刷新文件扩展名过滤字符串
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub RefreshFilterString()
            _filterstr = Nothing
        End Sub


        ''' <summary>
        ''' 检查文件扩展名是否存在指定的集合中
        ''' </summary>
        ''' <param name="filename">要检查的文件名，如果为空（Nothing），则返回 false。</param>
        ''' <param name="collection">要建使用的集合</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function CheckExtensionContains(ByVal filename As String,
                                                collection As System.Configuration.CommaDelimitedStringCollection) As Boolean
            If Not String.IsNullOrEmpty(filename) Then
                Dim ext = IO.Path.GetExtension(filename)
                If Not String.IsNullOrEmpty(ext) AndAlso
                    collection.Contains(ext) Then
                    Return True
                End If
            End If
            Return False
        End Function


        ''' <summary>
        ''' 检查文件扩展名是否已知
        ''' </summary>
        ''' <param name="filename">要检查的文件名，如果为空（Nothing），则返回 false。</param>
        ''' <returns>当文件扩展名已知时返回True ，否则 返回False</returns>
        ''' <remarks></remarks>
        Public Function IsKnowFileExtension(filename As String) As Boolean
            Return CheckExtensionContains(filename, SettingManager.Settings.KnowFileExtensions)
        End Function

        ''' <summary>
        ''' 检查文件扩展名是否不应该打开
        ''' </summary>
        ''' <param name="filename"></param>
        ''' <returns>一个布尔值，</returns>
        ''' <remarks>
        ''' 本软件无法编辑存在这个列表中的文件类型，
        ''' 这里的文件需要专门的工具才可以打开。
        ''' </remarks>
        Public Function IsNotbeOpendFileExtension(filename As String) As Boolean
            Return CheckExtensionContains(filename, SettingManager.Settings.NotbeOpendFileExtensions)
        End Function



    End Module

End Namespace

